﻿using System;
using System.Globalization;
using System.Windows.Input;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Tanmia.Basis.ViewInterface;

namespace Tanmia.Basis.ViewModel
{
    public abstract class BaseViewModel : NotificationObject, IConfirmNavigationRequest, IDisposable, INavigationAware
    {
        /// <summary>
        /// Gets the command to invoke when (Close) button is pressed.
        /// </summary>
        public ICommand CloseCommand { get; protected set; }

        /// <summary>
        /// Gets a reference to the MainWindow in the Shell Project.
        /// </summary>
        protected IShellView ShellView { get; private set; }

        public ViewInfo ViewInfo { get; protected set; }

        public BaseViewModel(IShellView shellView)
        {
            this.ShellView = shellView;

            // x1. Populate ViewInfo
            ViewInfo = ViewInfoAttribute.GetViewInfo(GetType());
        }

        public virtual void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            continuationCallback(true);
        }

        public virtual bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public virtual void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

        public virtual void OnNavigatedTo(NavigationContext navigationContext)
        {
            
        }

        public virtual void Dispose()
        {

        }

        #region Protected methods

        /// <summary>
        /// Update the status bar of the MainWindow.
        /// </summary>
        /// <param name="result">Operation result.</param>
        /// <param name="succeedResource">Operation succedd message to show.</param>
        /// <param name="unsucceedResource">Operation unsucceed message to show.</param>
        /// <param name="parameters">Parameters array of the resource message.</param>
        /// <remarks>
        /// This method depend of IShellView interface.
        /// </remarks>
        protected void UpdateStatus(bool result, string succeedResource, string unsucceedResource, params object[] parameters)
        {
            if (result)
                ShellView.UpdateStatus(string.Format(CultureInfo.CurrentCulture, succeedResource, parameters));
            else
                ShellView.UpdateStatus(string.Format(CultureInfo.CurrentCulture, unsucceedResource, parameters));
        }

        /// <summary>
        /// Update the status bar of the MainWindow.
        /// </summary>
        /// <param name="resource">Resource message name to use as template.</param>
        /// <param name="parameters">Parameters array of the resource message.</param>
        /// <remarks>
        /// This method depend of IShellView interface.
        /// </remarks>
        protected void UpdateStatus(string resource, params object[] parameters)
        {
            ShellView.UpdateStatus(string.Format(CultureInfo.CurrentCulture, resource, parameters));
        }

        #endregion Protected methods
    }
}
